﻿using CLVisionHalconNC620.Transform;
using HalconDotNet;
using NeuroCheck.PlugInLib.API;
using NeuroCheck.PlugInLib.Base;
using NeuroCheck.PlugInLib.ImageProcessing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CLVisionHalconNC620
{
    public class ExcuteHalcon_MultiThread
    {
        public static HTuple _InputIconicNames;
        public static HTuple _InputCtrlNames;
        public static HTuple _OutputIconicNames;
        public static HTuple _OutputCtrlNames;
        public static List<string> _procedurePaths = new List<string>();

        static List<(string, string, string)> _list_iconic_Input;
        static List<(string, string, string)> _list_iconic_Output;
        static List<(string, string, string)> _list_control_Input;
        static List<(string, string, string)> _list_control_Output;
        static List<(string, string, List<string>, string)> _list_cfg;

        public static NcPIExecutionResultInfo Excute(ExcuteHalconProc_MultiThreadParameter m_Parameter, NcPICheckFunctionBase ncPICheckFunctionBase)
        {
            try
            {
                List<HObject> inputHobj = new List<HObject>();
                HTupleVector inputHtuple = new HTupleVector(1);

                //读取设置参数列表
                _list_iconic_Input = m_Parameter.Ls_iconic_Input;
                _list_iconic_Output = m_Parameter.Ls_iconic_Output;
                _list_control_Input = m_Parameter.Ls_control_Input;
                _list_control_Output = m_Parameter.Ls_control_Output;
                _list_cfg = m_Parameter.Ls_cfg_Par;


                //当前procedure名称
                string name = m_Parameter.CurrentProcedureName;

                AssemblyInstance.nC_Halcon_MultiThread.LoadProcedure(name, out int code1, out string text1);
                if (code1 == -1)
                {
                    throw new NcPIUserException($"加载hdvp文件{name}失败！{text1}");
                }

                AssemblyInstance.nC_Halcon_MultiThread.GetProcedureParNames(out int a, out string b);

                try
                {
                    //根据表格读取输入参数
                    ExcuteHalcon. ReadValue(_list_iconic_Input, inputHtuple, inputHobj);
                }
                catch (Exception ex)
                {
                    throw new NcPIUserException($"读取图像参数失败！{ex.Message}");
                }
                try
                {
                    ExcuteHalcon .ReadValue(_list_control_Input, inputHtuple, inputHobj);

                }
                catch (Exception ex)
                {
                    throw new NcPIUserException($"读取控制参数失败！{ex.Message}");
                }
                try
                {
                    ExcuteHalcon. ReadValue_cfg(_list_cfg, inputHtuple);
                }
                catch (Exception ex)
                {
                    throw new NcPIUserException($"读取cfg配置参数失败！{ex.Message}");
                }


                //开启线程
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        HTupleVector outputHtuple = new HTupleVector(1);

                        //执行Halcon
                        AssemblyInstance.nC_Halcon_MultiThread.CallProcedureNC(name, inputHobj.ToArray(), out HObject[] outputHobj,  inputHtuple,  out outputHtuple, out int code2, out string text2);

                        //根据表格处理输出结果
                        if (code2 == -1)
                        {
                            // throw new NcPIUserException($"执行失败！{text2}");
                        }
                    }
                    catch (Exception ex)
                    {
                    }

                });
            }
            catch (Exception ex)
            {
                throw new NcPIUserException(ex.Message);

            }

            return NcPIExecutionResultInfo.OK;
        }
    }
}
